﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_DSDoanVienBiKhaiTru : System.Web.UI.Page
{
    readonly SecurityLib.Sec_AES _se = new SecurityLib.Sec_AES();
    public string st = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindGrid();
            st = _se.Decrypt(Request.QueryString["tid"].Replace(" ", "+"), "gold", 128);
        }

    }
    protected void Grid_DSDV_Xoa_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    //xóa đoàn viên trưởng thành
    protected void Btn_xoadoanvien_Click(object sender, EventArgs e)
    {

        CheckBox select;
        string id = "";
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            select = (CheckBox)dr.FindControl("chkchon");
            if (select.Checked == true)
            {
                id = Grid_DSDV_Xoa.DataKeys[dr.RowIndex].Value.ToString().Trim();
                CatalogAccess.Xoa_PhucHoi_DoanVien(id, true);
            }
        }
        BindGrid();
    }
    //load du lieu vao gridview
    private void BindGrid()
    {
        _dbDataContext _db = new _dbDataContext();
        string donvi = _se.Decrypt(Request.QueryString["dvid"].Replace(" ", "+"), "gold", 128); //MemberShip_ASP.GetMaDonViFromUserName(Page.User.Identity.Name);
        var val = from p in _db.tblDoanViens.Where(r => r.TrangThai == 5 && r.TrangThaiXoa == false && r.TrucThuoc.Trim() == donvi)
                  select new
                  {
                      MaDoanVien = p.MaDonVien.Trim(),
                      TenDoanVien = p.HoTen.Trim(),
                      Phai = (p.Phai == true ? "Nam" : "Nữ"),
                      NgaySinh = string.Format("{0:dd/MM/yyyy}", p.NgaySinh),
                      CMND = p.CMND.Trim(),
                  };
        Grid_DSDV_Xoa.DataSource = val;
        Grid_DSDV_Xoa.DataBind();
    }
    //khôi phục doan vien truong thanh:ID=3->doan vien:ID=1 chekcbox
    protected void Btn_khoiphucdoanvien_Click(object sender, EventArgs e)
    {

        CheckBox select;
        string id = "";
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            select = (CheckBox)dr.FindControl("chkchon");
            if (select.Checked == true)
            {
                id = Grid_DSDV_Xoa.DataKeys[dr.RowIndex].Value.ToString().Trim();
                CatalogAccess.CapNhatTrangThaiDoanVien(id, 1);
                try
                {
                    _dbDataContext _db = new _dbDataContext();
                    var dv = _db.tblTruongThanhKhaiTrus.Single(r => r.MaDoanVien.Trim() == id && r.TrungThanh == false);
                    _db.tblTruongThanhKhaiTrus.DeleteOnSubmit(dv);
                    _db.SubmitChanges();
                }
                catch { }
            }

        }
        BindGrid();

    }
    //xóa đoàn viên trưởng thành được chọn=checkbox
    protected void LinkButtonxoa_Click(object sender, EventArgs e)
    {
        CheckBox select;
        string id = "";
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            select = (CheckBox)dr.FindControl("chkchon");
            if (select.Checked == true)
            {
                id = Grid_DSDV_Xoa.DataKeys[dr.RowIndex].Value.ToString().Trim();
                CatalogAccess.Xoa_PhucHoi_DoanVien(id, true);


            }
        }

        BindGrid();
    }
    //xoa doan vien vinh vien:từng đoàn viên 1=trực tiếp trên gridview
    protected void Grid_DSDV_Xoa_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        string id = Grid_DSDV_Xoa.DataKeys[e.RowIndex].Value.ToString();
        if (CatalogAccess.Xoa_PhucHoi_DoanVien(id, true))
        {
            BindGrid();

        }
        else
        {
            Response.Write("<script>alert('Error!');</script>");
        }

    }
    //khoi phuc từng đoàn viên một=trực tiếp trên gridview
    protected void Grid_DSDV_Xoa_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        string id = Grid_DSDV_Xoa.DataKeys[e.RowIndex].Value.ToString();
        CatalogAccess.CapNhatTrangThaiDoanVien(id, 1);
        try
        {
            _dbDataContext _db = new _dbDataContext();
            var dv = _db.tblTruongThanhKhaiTrus.Single(r => r.MaDoanVien.Trim() == id && r.TrungThanh == false);
            _db.tblTruongThanhKhaiTrus.DeleteOnSubmit(dv);
            _db.SubmitChanges();
        }
        catch { }
        BindGrid();


    }
    //chọn all checkbox
    protected void chkInGridviewHeader_CheckedChanged(object sender, EventArgs e)//day la ham de goi trong header cua itemtemplate
    {
        CheckBox chk = (CheckBox)sender;
        CheckBox chkitem;
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            chkitem = (CheckBox)dr.FindControl("chkchon");
            if (chk.Checked == true)
            {
                chkitem.Checked = true;
            }
            else
            {
                chkitem.Checked = false;
            }
        }
    }
    //Eport to Excel
    public void ExporttoExcel(object sender, EventArgs e)
    {
        Session.Remove("ListMaDoanVien_Print");
        Session.Remove("TenDonVi_Print");
        string listmadoanvien = "";
        CheckBox ckh;
        Label lbl;
        int count = 0;
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            ckh = (CheckBox)dr.FindControl("chkchon");
            if (ckh.Checked == true)
            {
                lbl = (Label)dr.FindControl("lblMaDoanVien");
                listmadoanvien += lbl.Text.Trim() + "!";
                count++;
            }
        }
        if (count > 0)
        {
            Session["ListMaDoanVien_Print"] = listmadoanvien;
            Session["TenDonVi_Print"] = _se.Decrypt(Request.QueryString["tid"].Replace(" ", "+"), "gold", 128);
            Response.Redirect("javascript:window.open('../Print/PrintPage.aspx','_blank');");
        }
        else
        {
            SecurityLib.ToolsMore.ShowPopUpMsg("Chưa chọn đoàn viên để in.", this.Page);
        }
    }
    //chueyenr sinh hoạt đoàn viên
    protected void Btn_chuyensinhhoatdoan_Click(object sender, EventArgs e)
    {
        CheckBox select;
        string id = "";
        string tendoanvien = "";
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            select = (CheckBox)dr.FindControl("chkchon");
            if (select.Checked == true)
            {
                id = Grid_DSDV_Xoa.DataKeys[dr.RowIndex].Value.ToString().Trim();
                tendoanvien = ((Label)dr.FindControl("lblTenDoanVien")).Text;
                break;
            }
        }
        if (id != "")
        {
            SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
            Response.Redirect(PageURL.local + "DanhMuc/ChuyenSinhHoatDoan.aspx?id=" + _ps.Encrypt("4", "gold", 128) + "&subid=" + _ps.Encrypt("2", "gold", 128) + "&codeunionists=" + _ps.Encrypt(id.ToString(), "gold", 128) + "&nameunionists=" + _ps.Encrypt(tendoanvien, "gold", 128));
        }
        else
        {
            lblstatus.Visible = true;
            lblstatus.ForeColor = System.Drawing.Color.Red;
            lblstatus.Text = "Hãy chọn một đoàn viên trước khi chuyển!";
        }
    }

    protected void Grid_DSDV_Xoa_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "cmdKhaiTruDoanVien")
        {
            pndetail.Visible = true;
            pncontent.Visible = false;
            string madoanvien = e.CommandArgument.ToString();
            _dbDataContext _db = new _dbDataContext();
            var dv = _db.tblTruongThanhKhaiTrus.Single(r=>r.MaDoanVien.Trim()==madoanvien && r.TrungThanh==false);
            txtTenDoanVien.Text = dv.tblDoanVien.HoTen;
            txtNghiquyet.Text = dv.NghiQuyet;
            txtLyDo.Text = dv.LyDo;
            txtNgayKy.Text = string.Format("{0:dd/MM/yyyy}",dv.Ngay);
        }
    }
    protected void linkclose_Click(object sender, EventArgs e)
    {
        pndetail.Visible = false;
        pncontent.Visible = true;
    }
}